package com.slipper.system.schedule.service;

import java.util.List;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.slipper.system.schedule.dao.ScheduleDao;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import com.slipper.system.api.schedule.bo.ScheduleBO;
import com.slipper.system.api.scheduled.service.IScheduledSv;
import com.slipper.common.transaction.ReadTransactional;
import com.slipper.common.transaction.WriteTransactional;
import com.slipper.common.util.Tool;

@Service
@ReadTransactional
public class ScheduleService {
    @Resource
    private ScheduleDao scheduleDao;
    @Resource
    private IScheduledSv scheduledSv;
    
    public ScheduleBO queryBy(ScheduleBO bo){
        return scheduleDao.queryBy(bo);
    }
    
    public PageInfo<ScheduleBO> queryPage(ScheduleBO bo){
        PageHelper.startPage(bo.getPageNumber(), bo.getPageSize());
        return new PageInfo<>(scheduleDao.queryList(bo));
    }
    
    @WriteTransactional
    public Integer save(ScheduleBO bo){
        int result;
        if (bo.getStatus() == null) {
            bo.setStatus(ScheduleBO.STATUS_PAUSE);
        }
        if (bo.getId() != null && !"".equals(bo.getId())) {
            result = scheduleDao.update(bo);
        } else {
            bo.setId(Tool.getUUID());
            result = scheduleDao.insert(bo);
        }
        
        scheduledSv.createScheduleJob(bo);
        return result;
    }
    
    @WriteTransactional
    public Integer delete(ScheduleBO bo){
        for (String id : bo.getIds()) {
            ScheduleBO scheduleBO = new ScheduleBO();
            scheduleBO.setId(id);
            
            scheduledSv.deleteScheduleJob(scheduleBO);
        }
        return scheduleDao.delete(bo);
    }

    public List<ScheduleBO> queryList(ScheduleBO bo) {
        return scheduleDao.queryList(bo);
    }
    
    public int runTask(ScheduleBO bo) {
        for (String id : bo.getIds()) {
            ScheduleBO qo = new ScheduleBO();
            qo.setId(id);
            ScheduleBO scheduleBO = this.scheduleDao.queryBy(qo);
            
            scheduledSv.run(scheduleBO);
        }
        return 1;
    }
    
    @WriteTransactional
    public int updateStatus(ScheduleBO bo) {
        for (String id : bo.getIds()) {
            ScheduleBO scheduleBO = new ScheduleBO();
            scheduleBO.setId(id);
            ScheduleBO task = this.scheduleDao.queryBy(scheduleBO);
            
            if (ScheduleBO.STATUS_NORMAL.equals(bo.getStatus())) { // 恢复任务
                scheduledSv.resumeJob(task);
            } else if (ScheduleBO.STATUS_PAUSE.equals(bo.getStatus())) { // 暂停任务
                scheduledSv.pauseJob(task);
            } else {
                continue;
            }
            
            task.setStatus(bo.getStatus());
            this.scheduleDao.update(task);
        }

        return 1;
    }

    public ScheduleBO queryOne(ScheduleBO bo) {
        return this.scheduleDao.queryOne(bo);
    }
}
